<?php
class model_visit extends framework_base_model {
	/**
	 * @desc 添加走访信息
	 * @param array 
	 * @return number
	 */
	public static function visitAdd($data) {
    	if(!is_array($data)) {
    		return 0;
    	}
    	try{
    		$sql = "insert into `weixin_visit_log` set ";
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql .= 'raw_add_time=NOW()';
    		$arr=self::DB() -> dbh -> prepare($sql);
    		$rel = $arr->execute($binds);
    		if($rel == TRUE) {
    			return self::DB()->dbh->lastInsertId();
    		} else {
    			return 0;
    		}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    }
    /*
     * 根据ID获取访问信息
     */
    public static function getVisitInfoById($id) {
    	$info = array();
    	if(intval($id) <= 0) {
    		return $info;
    	}
    	$sql = "select * from `weixin_visit_log` where visit_log_id=:visit_log_id and delete_flag = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(":visit_log_id" => intval($id)));
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
    /**
     * @desc 更新走访记录
     * @param unknown $id
     * @param unknown $data
     * @return unknown|number
     */
    public static function updateVisitInfo($id,$data) {
    	try{
    		$sql = 'UPDATE `weixin_visit_log` SET ';
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$ids = implode(',',$id);
    		$sql = trim($sql,',');
    		$sql .= ' WHERE `visit_log_id` in ('.$ids.')';
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute($binds);
    		$rowCount = $stm -> rowCount();
    		return $rowCount;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    }
    /**
     * 获取走访列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function visitList($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "select `visit_log_id`,`visit_unit`,`visit_addr`,`recevie_name`,`recevie_study`,`recevie_mobile`,`recevie_email`,`recevie_date`,`recevie_facts`,`recevie_question`,`recevice_mind`,`handle_msg`,`handle_date`,`sales_id`,`sales_name`,`sales_area`,`sales_part`,`sales_produce`,`author`,`editer`,`raw_add_time` from `weixin_visit_log` where `delete_flag` = 0";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['sales_name'])) {
    			$where .= " and sales_name like '%{$condition['sales_name']}%'";
    		}
			if(isset($condition['visit_unit'])) {
    			$where .= " and visit_unit like '%{$condition['visit_unit']}%'";
    		}
    	}
    	$where .= " order by `raw_add_time` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select count(*) from `weixin_visit_log` where `delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	/*
	* @desc 批量写入订单
	* @author kevin wang
	* @param array
	* @return boolean
	*
	*/
	public static function batch_insert_order(array $orders){
		if(empty($orders)){
			return false;
		}
		
		try{
			$sql = "";
			$i = 0;
			foreach($orders as $order){
				$sql .= "insert into `weixin_order` set ";
				foreach($order as $k=>$v){
					$key = self::$_batch_keywords[$k -1];
					$sql .= "`{$key}` = '{$v}',";
				}
				$sql .= 'raw_add_time=NOW();';
				$i++;
				if($i > 99){
					$sqls[] = $sql;
					$sql = "";
					$i = 0;
				}
				
			}
			if(!empty($sql)){
				$sqls[] = $sql;
			}

			$pdo = self::DB() -> dbh;

			foreach($sqls as $sqlv){
				
				$stm = $pdo -> prepare($sqlv);
    			$stm -> execute();
			}
			
			return true;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
}
